package com.itheima.ydd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.PageResult;
import com.itheima.ydd.common.Result;
import com.itheima.ydd.dto.YddAdvimgDto;
import com.itheima.ydd.entity.YddAdvimg;
import com.itheima.ydd.entity.YddAdvpos;
import com.itheima.ydd.service.YddAdvimgService;
import com.itheima.ydd.service.YddAdvposService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/ydd_advimg")
@Slf4j
public class YddAdvimgController {
    @Autowired
    YddAdvimgService yddAdvimgService;
    @Autowired
    YddAdvposService yddAdvposService;

    //广告图分页
    //@Cacheable(value = "ydd_advimg", key = "yddadvimg",condition = "#page != null")
    @GetMapping("/list")
    public Result page(String advimgdedc, String advimfpos, int page, int limit) {
        //分页构造器
        Page<YddAdvimg> pageInfo = new Page<>(page, limit);
        Page<YddAdvimgDto> dtoPage = new Page<>();
        //条件构造器
        LambdaQueryWrapper<YddAdvimg> lqw = new LambdaQueryWrapper<>();
        //模糊查询
        lqw.like(advimgdedc != null, YddAdvimg::getAdvimgdesc, advimgdedc);
        lqw.like(advimfpos != null, YddAdvimg::getAdvimgpos, advimfpos);
        //排序
        lqw.orderByDesc(YddAdvimg::getId);
        //调用service
        Page<YddAdvimg> page1 = yddAdvimgService.page(pageInfo, lqw);

        BeanUtils.copyProperties(pageInfo, dtoPage, "records");
        List<YddAdvimg> records = page1.getRecords();

        List<YddAdvimgDto> list = records.stream().map(item -> {
            YddAdvimgDto yddAdvimgDto = new YddAdvimgDto();
             String advimgpos = item.getAdvimgpos();
            BeanUtils.copyProperties(item, yddAdvimgDto);
            YddAdvpos yddAdvpos = yddAdvposService.getById(advimgpos);
            yddAdvimgDto.setAdvposname(yddAdvpos.getAdvposname());
            return yddAdvimgDto;
        }).collect(Collectors.toList());
        dtoPage.setRecords(list);

        return pageInfo != null ? Result.ok("", PageResult.answer(dtoPage.getTotal(), dtoPage.getSize(), dtoPage.getCurrent(), dtoPage.getRecords())) : Result.error("暂无标签数据");
    }

    //广告图详情
    @GetMapping("/rowInfo")
    public Result selectById(Long id) {
        log.info("广告图回显 id:{}", id);
        YddAdvimg yddAdvimg = yddAdvimgService.getById(id);
        return Result.ok("", yddAdvimg);
    }

    //广告图修改
    //@CacheEvict(value = "ydd_advimg", allEntries = true)
    @PostMapping("/update")
    public Result update(@RequestBody YddAdvimg advimg) {
        log.info("广告图修改 advimg:{}", advimg);
        yddAdvimgService.updateById(advimg);
        return Result.ok("", advimg);
    }

    //广告图删除
    //@CacheEvict(value = "ydd_advimg", allEntries = true)
    @GetMapping("/del")
    public Result delete(long id) {
        log.info("广告图删除 id:{}", id);
        yddAdvimgService.removeById(id);
        return Result.ok("", id);
    }

    //广告图添加
    //@CacheEvict(value = "ydd_advimg", allEntries = true)
    @PostMapping("/add")
    public Result add(@RequestBody YddAdvimg advimg) {
        log.info("广告图添加 advimg:{}", advimg);
        yddAdvimgService.save(advimg);
        return Result.ok("", advimg);
    }

}
